import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/store/config_store.dart';
import 'package:bxultimate/widgets/app/app_article_card.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class ApplicationNewsIndexPage extends StatefulWidget {
  const ApplicationNewsIndexPage({Key? key}) : super(key: key);

  @override
  State<ApplicationNewsIndexPage> createState() =>
      _ApplicationNewsIndexPageState();
}

class _ApplicationNewsIndexPageState extends State<ApplicationNewsIndexPage> {
  final controller = Get.find<ApplicationNewsIndexController>();
  final state = Get.find<ApplicationNewsIndexController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: AppBar(
          title: Obx(() => Text('${state.title}')),
        ),
        body: EasyRefresh(
          // 下拉样式
          header: TDRefreshHeader(),
          footer: TaurusFooter(),
          child: Padding(
            padding: EdgeInsets.only(
                left: AppTheme.iDefaultPadding,
                right: AppTheme.iDefaultPadding),
            child: Obx(() => Column(children: [
                  state.carouselList.length == 0
                      ? SizedBox()
                      : Container(
                          height: 160,
                          child: Swiper(
                            itemCount: state.carouselList.length,
                            pagination: const SwiperPagination(
                                alignment: Alignment.bottomCenter,
                                builder: TDSwiperPagination.dotsBar),
                            itemBuilder: (BuildContext context, int index) {
                              return TDImage(
                                imgUrl: state.carouselList[index].imgUrl,
                              );
                            },
                          )),
                  Gaps.vGap12,
                  _buildModule(),
                  Gaps.vGap12,
                  ...state.articleList
                      .map((e) => AppArticleCard(
                            article: e,
                            onTap: () {
                              controller.toArticleDetail(e);
                            },
                          ))
                      .toList()
                ])),
          ),
          // 下拉刷新回调
          onRefresh: controller.onRefresh,
          onLoad: controller.onLoad,
        ),
      ),
    );
  }

  _buildModule() {
    return Container(
      width: Get.width,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(AppTheme.iDefaultRadius),
      ),
      child: Wrap(
        alignment: WrapAlignment.start,
        children: [
          for (int i = 0; i < state.newsCategory.length; i++)
            _buildModuleItem(i),
        ],
      ),
    );
  }

  _buildModuleItem(index) {
    return GestureDetector(
      onTap: (){
        Get.toNamed(RouterName.preServiceEducationPolicyNews,
            arguments: {"title": "${state.newsCategory[index].name}", "typeId": state.newsCategory[index].id});
      },
      child: Container(
          width: (Get.width - AppTheme.iDefaultPadding * 2) / 5,
          padding: EdgeInsets.all(AppTheme.iDefaultPadding),
          child: Column(children: [
            MkLoadImage(
              ConfigStore.to.baseFileUrl + state.newsCategory[index].alias!,
              width: 40,
              height: 40,
            ),
            Gaps.vGap8,
            Text(
              state.newsCategory[index].name!,
              style: TextStyle(fontSize: 10),
            )
          ])),
    );
  }

  @override
  void dispose() {
    Get.delete<ApplicationNewsIndexController>();
    super.dispose();
  }
}
